from pyx_package.script import BaseBehavior
from pyx_package.ps_paramete import PsParameter

PYX_MODULE_NAME = "delete-files"

import os
import shutil
import platform
import subprocess

class ScriptBehavior(BaseBehavior):

    def __init__(self) -> None:
        super().__init__()
        
        self.SetExeMode("function")

        self.p_path = self.CreateParam("path", "被删除的文件路径", PsParameter.Input, str, "")


        if ('Windows' == platform.system()):
            print('Windows')
        elif ('Linux' == platform.system()):
            print('Linux')
        else:
            print('Others')


    def DoStep(self):

        file_path = self.p_path.value

        try:

            if os.path.isdir(file_path):
                shutil.rmtree(file_path)

            elif os.path.isfile(file_path):
                if 'Windows' == platform.system():
                    os.remove(file_path)
                elif 'Linux' == platform.system():
                    os.unlink(file_path)

            else:
                self.Debug("file is not a dir/file : " + file_path, "error")

        except:
        
            remove_cmd = "DEL /F /A /Q \\\\?\\\"" + file_path + "\" & "
            remove_cmd += "RD /S /Q \\\\?\\\"" + file_path  + "\""
            popen = subprocess.Popen(remove_cmd, shell=True)
            popen.wait()
            if popen.poll() != 0:
                self.Debug("file delete error: " + file_path, "error")
